#!/bin/bash
# Author: fanyongfeng
# 0 1 * * * /bin/bash /root/script/mongo_data_backup.sh &>> /backup/mongo_data_backup/backup.log

TODAY=`date +%Y%m%d`
BACKUP_DIR=/backup/mongo_data_backup
LOCAL_IP=`ip a | grep inet | grep brd | grep -Ev 'docker|cni|dns' | awk -F'/' '{print $1}' | awk '{print $2}' | head -1`
MONGO_USER_INFO_FILE="/root/script/.mongo_user_info"
[[ ! -f ${MONGO_USER_INFO_FILE} ]] && echo "file not exist: ${MONGO_USER_INFO_FILE}" && exit 1
USERNAME=$(awk '{print $1}' ${MONGO_USER_INFO_FILE})
PASSWORD=$(awk '{print $2}' ${MONGO_USER_INFO_FILE})

mkdir -p ${BACKUP_DIR}/${TODAY}
mongodump -h ${LOCAL_IP} -u ${USERNAME} -p ${PASSWORD} -d admin -o ${BACKUP_DIR}/${TODAY}
[[ $? -eq 0 ]] && echo "${TODAY} mongo backup success" || echo "${TODAY} mongo backup failed"

find ${BACKUP_DIR} -type d -mtime +180 | xargs -r -n 10 rm -rf


